
#include <gl/glut.h>
#include "GLUtil.h"
#include <vec.h>
#include "Polygon.h"
#include <vector>
#include <malloc.h>
#include <math.h>
#include "ConvertUtil.h"
#include "SDM.h"

using namespace std;

#pragma comment(lib, "imager")


static GLint    ImageWidth=800;
static GLint    ImageHeight=800;

bool compute = true;

double colorScale = 0.1;

SDM* mine;

double xInf, yInf, zInf, xSup, ySup, zSup;

int xRes, yRes, zRes;

double xStep, yStep, zStep;

double mapDepth(double v)
{
	v *= -10;
	//printf("%f\n",v);
	return sqrt(exp(v)) * colorScale;
}

void TrenderMesh()
{
	int w = xRes + 1;

	int x, y;

	x = y = 0;

	Value v;	

	//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	glBegin(GL_TRIANGLES);

	for (double i=xInf; i<xSup; i+=xStep)
	{
		y = 0;

		for (double j=yInf; j<ySup; j+=yStep)
		{
			Vec3d va(i - xStep / 2, j - yStep / 2, 0);
			Vec3d vb(i + xStep / 2, j - yStep / 2, 0);
			Vec3d vc(i + xStep / 2, j + yStep / 2, 0);
			Vec3d vd(i - xStep / 2, j + yStep / 2, 0);

			if (compute)
			{
				v = mine->getEvaluation(Vec2d(va[0], va[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(va[0], va[1], va[2]);
			if (compute)
			{
				v = mine->getEvaluation(Vec2d(vb[0], vb[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(vb[0], vb[1], vb[2]);
			if (compute)
			{
				v = mine->getEvaluation(Vec2d(vc[0], vc[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(vc[0], vc[1], vc[2]);


			if (compute)
			{
				v = mine->getEvaluation(Vec2d(va[0], va[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(va[0], va[1], va[2]);
			if (compute)
			{
				v = mine->getEvaluation(Vec2d(vc[0], vc[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(vc[0], vc[1], vc[2]);
			if (compute)
			{
				v = mine->getEvaluation(Vec2d(vd[0], vd[1]), false);
			}			
			v.d = mapDepth(v.d);
			glColor3d(v.d, 0, 1);
			glVertex3d(vd[0], vd[1], vd[2]);
		}

		x++;
	}

	//compute = false;

	glEnd();


}

void TrenderVector()
{
	Value v;

	glColor3d(1, 1, 1);

	for (double i=xInf; i<xSup; i+=xStep)
	{
		for (double j=yInf; j<ySup; j+=yStep)
		{
			Vec2d start(i, j);

			v = mine->getEvaluation(start, true);

			Vec2d end = v.n * 0.05 + start;

			if (v.d > 0)
			{
				continue;
			}

			glBegin(GL_LINES);

			glVertex3d(start[0], start[1], 0);
			glVertex3d(end[0], end[1], 0);

			glEnd();
		}
	}

	glPointSize(5);

	glColor3d(1, 0, 0);

	for (double i=xInf; i<xSup; i+=xStep)
	{
		for (double j=yInf; j<ySup; j+=yStep)
		{
			Vec2d start(i, j);

			v = mine->getEvaluation(start, true);


			if (v.d > 0)
			{
				continue;
			}

			glBegin(GL_POINTS);

			glVertex3d(start[0], start[1], 0);				

			glEnd();
		}
	}
}

void TinitializeMesh(Edge** edgeList, Vert** vertList, int* edgeCount, int* vertCount)
{
	conversionFromObj( edgeList, vertList, edgeCount, vertCount );
}

void _main()
{	

	xRes = 40;
	yRes = 40;
	
	xInf = -1;
	yInf = -1;
	
	xSup = 2;
	ySup = 2;
	
	xStep = (xSup - xInf) / (xRes - 1);
	yStep = (ySup - yInf) / (yRes - 1);
	
	Polygon* poly = new Polygon();
	poly->buildPolyStructure(conversionFromObj);
	mine = new GridSDM(poly, 0.01);	

	poly->getInclusionInfo(Vec2d(0.5, 0.7));

	Value v = mine->getEvaluation(Vec2d(0.8, 0.9), false);

	printf("%f\n", v.d);

	//colorBuffer = (double*)malloc((xRes + 1) * (yRes + 1) * sizeof(double));	

	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(ImageWidth, ImageHeight);
	glutCreateWindow("FLIP");
	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);
	glutKeyboardFunc(keyboard);

	glutMainLoop();

}

